<?php
namespace Home\Model;
use Think\Model;
class ShopModel extends Model 
{

	public $tableName = 'shop';

	/*获取入驻商家*/
	public function getShop($id, $tableName)
	{
		$shop['shop_id'] = $id['shop_id'];
		$shopModel     = M($tableName);
		$shop_res      = $shopModel->where($shop)->select();
		return $shop_res;
	}

	/*审核通过*/
	public function via($id)
	{
		$shop['shop_id'] = $id['shop_id'];
		$shopModel     = M($this->tableName);

		$newShop['apply'] = 1;
		$newShop['isApply'] = 1;
		$shop_res      = $shopModel->data($newShop)->where($shop)->save();
		return $shop_res;
	}

	/*审核不通过*/
	public function noVia($id)
	{
		$shop['shop_id'] = $id['shop_id'];
		$shopModel     = M($this->tableName);

		$newShop['apply'] = 0;
		$newShop['isApply'] = 1;
		$res      = $shopModel->data($newShop)->where($shop)->save();
		return $res;
	}

	/*删除店铺*/
	public function del($id)
	{
		$shop['shop_id'] = $id['shop_id'];
		$shopModel     = M($this->tableName);
		$shop_res      = $shopModel->where($shop)->delete();
		return $shop_res;
	}

	/*更改店铺信息*/
	public function changed($arr)
	{
		$shopModel    	 = M($this->tableName);
		$shop['shop_id'] = $arr['shop_id'];
		
		$saveShop = $shopModel->data($arr)->where($shop)->save();
		return $saveShop;

	}

	/*获取你的下级*/
	public function myfans($id, $tableName)
	{
		$res = array();
		$res_user = array();
		$arr['pid'] = $id;

		$Model = M($tableName);
		$new_user = $Model->where($arr)->select();

		for ($i=0; $i < count($new_user); $i++) 
		{
			if ($new_user[$i]['is_user'] == 0) 
			{
				//获取下级
				$res_user[$i] = $new_user[$i];
			}
		}

		$res = $res_user;
		
		return $res;
	}

	/*获取店铺的上级*/
	public function level($id)
	{
		$arr['shop_id'] = $id;

		$shopModel = M('shop');
		$new_user = $shopModel->where($arr)->select();
		
		if($new_user[0]['is_user'])
		{
			$user['user_Id'] = $new_user[0]['pid'];

			$userModel = M('user');
			$puser = $userModel->where($user)->select();
			if($puser)
			{
				$puser[0]['type'] = '会员';
			}

		}else
		{
			$shop['shop_id'] = $new_user[0]['pid'];

			$puser = $shopModel->where($shop)->select();
			if($puser)
			{
				$puser[0]['type'] = '商家';
			}
		}
		return $puser;
		//var_dump($puser);exit();
	}



	///////////////////////
	/*获取店铺信息*/
	public function shopUser($id)
	{
		//print_r($this->tableName);
		$shopModel    = M($this->tableName);
		$shop['shop_id'] = $id;
		$res          = $shopModel->where($shop)->limit(1)->select();
		
		return $res;
	}


	/*计算商家积分*/
	public function points($id)
	{
		$rechargeModel = M('recharge');
		$shopRecharge['shop_id'] = $id;
		$shopRecharge['is_dl'] = 0;
		$shopRecharge['is_tj'] = 0;
        $rechargeRes = $rechargeModel->where($shopRecharge)->select();
        $length = count($rechargeRes);

        $num = 0;
        for ($i=0; $i < $length; $i++) { 
            $num = $num + $rechargeRes[$i]['points'];
        }
        

		$shopModel = M('shop');
        $shop['shop_points'] = $num;
        $saveShop = $shopModel->data($shop)->where($shopRecharge)->save();
	}

	/*给会员充值积分*/
	public function givePoints($arr)
	{
		$space = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $arr['phone']);
		if($space)
		{
			$userModel = M('user');
			$hasUser = $userModel->where('phone = '.$arr['phone'])->select();

			if($hasUser)
			{
				//获取店铺信息
				$res = $this->shopUser($arr['shop_id']);
				
				$number = $arr['number'];
				//print_r($res);
				if($res[0]['shop_points'] < $number)
				{
					return 'noNumber';//余额不足
				}else
				{
					/*商家赠送积分的上级---区域代理--*/
					$proxy = $this->proxy($arr['shop_id'], $arr['number']);
					/*区域代理end*/

					$rechargeModel = M('recharge');
					$recharge['shop_id'] = $arr['shop_id'];
					$recharge['user_Id'] = $hasUser[0]['user_id'];
					$recharge['points'] = -$arr['number'];
					$recharge['reason'] = $arr['reason'];
        			$recharge['shijian'] = date('Y-m-d H:i:s',time());

					$recharge_in = $rechargeModel->data($recharge)->add();
					
					/*计算积分*/
		            $points = $this->points($arr['shop_id']);
		            
					if($recharge_in)
					{
						/*充值日志*/
						$logModel = M('log');
						$log['shop_id'] = $arr['shop_id'];
						$log['user_Id'] = $hasUser[0]['user_id'];
						$log['points'] = +$arr['number'];
						$log['reason'] = '消费积分';
	        			$log['shijian'] = date('Y-m-d H:i:s',time());
	        			$log_in = $logModel->data($log)->add();
	        			/*会员积分单元变化记录*/
	        			$this->nowUnit($hasUser[0]['user_id']);
	        			/*会员积分单元变化记录end*/

	        			if($log_in)
						{
							/*计算会员信息*/
							uusePoints($log['user_Id']);
							/*计算会员信息end*/
							
							/*给会员的上级返积分*/
							$user['user_Id'] = $hasUser[0]['user_id'];
							$userModel1 = D( 'Admin/User' );
							$userRes1 = $userModel1->level($user['user_Id']);

							/*获取佣金*/
							$configModer = M('config');
							$configRes   = $configModer->select();
							$commision = $configRes[0]['commision'];
							//var_dump($commision);exit();
							if($userRes1[0]['type'] == '会员')
							{
								$logModel = M('log');
								$log1['user_Id'] = $userRes1[0]['user_id'];
								$log1['points'] = +$arr['number']*$commision;
								$log1['reason'] = '赠送积分';
								$log1['is_tj'] = 1;
								$log1['shijian'] = date('Y-m-d H:i:s',time());
								
								$log_in1 = $logModel->data($log1)->add();
								/*计算会员信息*/
								uusePoints($userRes1[0]['user_id']);
								/*计算会员信息end*/
							}elseif($userRes1[0]['type'] == '商家')
							{
								$rechargeModel1 = M('recharge');
								$recharge1['shop_id'] = $userRes1[0]['shop_id'];
								$recharge1['points'] = +$arr['number']*$commision;
								$recharge1['reason'] = '赠送积分';
								$recharge1['is_tj'] = 1;
								$recharge1['shijian'] = date('Y-m-d H:i:s',time());

								$recharge_in1 = $rechargeModel1->data($recharge1)->add();
							}
							/*给会员的上级返积分end*/

							/*给店铺的上级返积分*/
							$shop2['shop_id'] = $arr['shop_id'];
							$shopModel2 = D( 'Home/Shop' );
							$shopRes2 = $shopModel2->level($shop2['shop_id']);
								
							if($shopRes2[0]['type'] == '会员')
							{
								$logModel2 = M('log');
								
								$log2['user_Id'] = $shopRes2[0]['user_id'];
								$log2['points'] = +$arr['number']*$commision;
								$log2['reason'] = '赠送积分';
								$log2['is_tj'] = 1;
								$log2['shijian'] = date('Y-m-d H:i:s',time());
								
								$log_in2 = $logModel2->data($log2)->add();
								
								/*计算会员信息*/
								uusePoints($shopRes2[0]['user_id']);
								/*计算会员信息end*/
							}elseif($shopRes2[0]['type'] == '商家')
							{
								$rechargeModel2 = M('recharge');
								$recharge2['shop_id'] = $shopRes2[0]['shop_id'];
								$recharge2['points'] = +$arr['number']*$commision;
								$recharge2['reason'] = '赠送积分';
								$recharge2['is_tj'] = 1;
								$recharge2['shijian'] = date('Y-m-d H:i:s',time());

								$recharge_in2 = $rechargeModel2->data($recharge2)->add();
							}
							/*给店铺的上级返积分end*/
							
							return 'logSession';
		        			
						}else
						{
							return 'logErr';//会员充值失败
						}
					}else
					{
						return 'rechargeErr';//店铺充值失败
					}

				}
			}else
			{
				return 'notHas';//此会员不存在
			}
		}else
		{
			return 'notPhone';//电话不正确
		}
	}

	//会员各个积分单元记录
	public function nowUnit($userid)
	{
		$logModel = M('log');

		$arr['user_Id'] = $userid;
		$arr['reason'] = '消费积分';
		$res = $logModel->where($arr)->select();
		//定义一个变量，用于保存积分
		$log['pay_points'] = 0;
		for ($i=0; $i < count($res); $i++) { 
			$log['pay_points'] = $res[$i]['points'] + $log['pay_points'];
		}
		//计算有几个积分单元
		$configModer = M('config');
		$configRes   = $configModer->select();
	    $weight = $configRes[0]['weight'];
		
		$log['unit'] = floor($log['pay_points']/$weight);
		if($log['unit'] < 0)
		{
			$log['unit'] = 0;
		}
		$unitModel = M('unit');
		$unit['user_Id'] = $userid;
		$unit['now_unit'] = $log['unit'];
		$unit['shijian'] = date('Y-m-d H:i:s',time());

		$unit_in = $unitModel->data($unit)->add();

	}

	/*区域代理返积分*//*商家 区域代理*/
	public function proxy($id, $number)
	{
		$configModel = M('config');
		$hasConfig = $configModel->where('Id = 1')->select();
		$proxy = $hasConfig[0]['proxy'];
		
		$shopModel = M('shop');
		$hasShop = $shopModel->where('shop_id = '.$id)->select();
		
		if(!$hasShop[0]['is_proxy'])
		{
			$arr = $this->proxyIn($hasShop[0]['shop_id']);
		}else{
			$rechargeModel1 = M('recharge');
			$recharge1['shop_id'] = $hasShop[0]['shop_id'];
			$recharge1['points'] = +$number*$proxy;
			$recharge1['reason'] = '赠送积分';
			$recharge1['is_dl'] = 1;
			$recharge1['shijian'] = date('Y-m-d H:i:s',time());

			$recharge_in1 = $rechargeModel1->data($recharge1)->add();
		}
		
		if($arr[0]['type'] == '会员')
        {
        	$logModel = M('log');
			$log['user_Id'] = $arr[0]['user_id'];
			$log['points'] = +$number*$proxy;
			$log['reason'] = '赠送积分';
			$log['is_dl'] = 1;
			$log['shijian'] = date('Y-m-d H:i:s',time());
			$log_in = $logModel->data($log)->add();
			
			/*计算会员信息*/
			uusePoints($arr[0]['user_id']);
			/*计算会员信息end*/
			//var_dump($log_in);exit();
        }elseif($arr[0]['type'] == '商家')
        {
        	$rechargeModel = M('recharge');
			$recharge['shop_id'] = $arr[0]['shop_id'];
			$recharge['points'] = +$number*$proxy;
			$recharge['reason'] = '赠送积分';
			$recharge['is_dl'] = 1;
			$recharge['shijian'] = date('Y-m-d H:i:s',time());

			$recharge_in = $rechargeModel->data($recharge)->add();
        }
	}

	public function proxyIn($id)
	{
		$shop['shop_id'] = $id;
		$shopModel = D( 'Home/Shop' );
		
		$shopRes = $shopModel->level($shop['shop_id']);
		
		if($shopRes[0]['is_proxy'] == 1)
		{
			return $shopRes;
		}
		
	}
	
	

}